package cn.henu.dao;

import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import cn.henu.model.User;

@Repository("userDao")
public class UserDaoImpl extends UserBaseDao implements UserDao {

    public static final String SEQ_USER_NAME = "seq_user";

    private String getCountSql(User query) {
        StringBuffer sql = new StringBuffer("");

        sql.append(" select ");
        sql.append("  count(*) ");
        sql.append(" from ");
        sql.append(" creative ");
        sql.append(" where 1=1 ");
        sql.append(" and nvl(deleted,0) = 0 ");
        sql.append(" and PROJECT_ID=:projectId");

        return sql.toString();
    }

    @SuppressWarnings("deprecation")
    @Override
    public Long listUserCount(User user) {
        if (null == user) {
            return 0L;
        }
        String sql = getCountSql(user);
        return getDao().queryForLong(sql, new BeanPropertySqlParameterSource(user));
    }

    @Override
    public List<Map<String, Object>> listUser(User user) {
        List<Map<String, Object>> list;
        if (user == null) {
            return null;
        }
        String sql = "";
        SqlParameterSource param = new BeanPropertySqlParameterSource(user);
        list = getDao().queryForList(sql, param);
        return list;
    }

    @Override
    public User createUser(User user) {
        if (user == null) {
            return null;
        }

        Integer affectRow = 0;
        SqlParameterSource param = null;
        Long userId = getId(SEQ_USER_NAME);
        user.setUserId(userId);

        StringBuffer sql = new StringBuffer();
        //sql.append("insert into user");

        param = new BeanPropertySqlParameterSource(user);

        affectRow = getDao().update(sql.toString(), param);
        return affectRow > 0 ? user : null;
    }

    @Override
    public User updateUser(User user) {
        if (user == null) {
            return null;
        }
        StringBuffer sql = new StringBuffer();
        int affectRow = 0;
        SqlParameterSource param = null;
        sql.append("update user set name=:name where userId = :userId");
        param = new BeanPropertySqlParameterSource(user);
        affectRow = getDao().update(sql.toString(), param);

        return affectRow > 0 ? user : null;
    }

    @Override
    public Integer deleteUser(User user) {
        StringBuffer sql = new StringBuffer();
        sql.append("update user set deleted = 1 where id=:userId");
        SqlParameterSource param = null;
        param = new BeanPropertySqlParameterSource(user);

        return getDao().update(sql.toString(), param);
    }

    @Override
    public List<User> getUser(User user) {
        StringBuffer sql = new StringBuffer();
        sql.append("select * from user where deleted = 0 and id=:userId");
        BeanPropertyRowMapper<User> mapper = new ParameterizedBeanPropertyRowMapper<User>();
        mapper.setMappedClass(User.class);
        SqlParameterSource param = new BeanPropertySqlParameterSource(user);
        //return getSimpleJdbcTemplate().query(sql.toString(),mapper,param);
        return getDao().query(sql.toString(), param, mapper);
    }

}



























